package cn.sxt.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.LeaveBill;
import cn.sxt.sys.domain.User;
import cn.sxt.sys.service.LeaveBillService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.vo.LeaveBillVo;

@Controller
@RequestMapping("leaveBill")
public class LeaveBillController {

	@Autowired
	private LeaveBillService leaveBillService;

	// 跳到请假单管理leaveBillManager.jsp
	@RequestMapping("toLeaveBillManager")
	public String toLeaveBillManager() {
		return "system/leaveBill/leaveBillManager";
	}

	// 登陆请假单全查询，分页，模糊
	@RequestMapping("queryAllLeaveBill")
	@ResponseBody
	public DataGridView queryAllLeaveBill(LeaveBillVo leaveBillVo,HttpSession session) {
		User user=(User) session.getAttribute("user");
		//如果是超级管理员，那么查询所有的请假单
		if(user.getType()==SYS_Constast.USER_TYPE_SUPER) {
			return leaveBillService.queryAllLeaveBill(leaveBillVo);
		}else {
			//否则只能查自己
			leaveBillVo.setUserid(user.getId());
			return leaveBillService.queryAllLeaveBill(leaveBillVo);
		}
	}

	// 跳添加页面
	@RequestMapping("toAddLeaveBill")
	public String toAddLeaveBill() {
		return "system/leaveBill/leaveBillAdd";
	}

	// 添加
	@RequestMapping("addLeaveBill")
	@ResponseBody
	public Map<String, Object> addLeaveBill(LeaveBillVo leaveBillVo, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = leaveBillService.addLeaveBill(leaveBillVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateLeaveBill")
	public String toUpdateLeaveBill(LeaveBillVo leaveBillVo, Model model) {
		LeaveBill leaveBill = leaveBillService.queryLeaveBillById(leaveBillVo.getId());
		model.addAttribute("leaveBill", leaveBill);
		return "system/leaveBill/leaveBillUpdate";
	}

	// 修改
	@RequestMapping("updateLeaveBill")
	@ResponseBody
	public Map<String, Object> updateLeaveBill(LeaveBillVo leaveBillVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = leaveBillService.updateLeaveBill(leaveBillVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除请假单
	@RequestMapping("deleteLeaveBill")
	@ResponseBody
	public Map<String, Object> deleteLeaveBill(LeaveBillVo leaveBillVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = leaveBillService.deleteLeaveBillById(leaveBillVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

}
